package cn.edu.ctbu.datajpa01.service;

import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public List<Student> findAll(){

        return  studentRepository.findAll();
    }

    /**
     * 按id进行查询
     * @param id
     * @return
     */
       public Student findById(Integer id){

        return studentRepository.findById(id).orElse(null);
    }



    /**
     * 按name进行查询,like
     * @param name
     * @return
     */
    public List<Student> findByName(String name){

        return studentRepository.findByNameLike(name);
    }

    public List<Student> findByNameAndPassword(String name,String password){

        return studentRepository.findByNameAndPassword(name,password);
    }

    public void insert(Student student){
        studentRepository.save(student);
    }
}
